package com.bw.servlet;

import com.bw.dao.UserDao;
import com.bw.pojo.User;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/***************************
 FileAction:

 CreateTime:2022-05-10 9:53
 Author:Aaking-aline
 ****************************/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @SneakyThrows
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // beanutils 这个工具包可以帮助我们 将大量的 前端发过来的字段 转换成 pojo对象
        // getParameterMap是可以 一次获取所有前端发送过来的 字段并以键值对的形式存入map集合中
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();

        BeanUtils.populate(user,map);  // 映射完毕

        new UserDao().insertUser(user);

        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}
